<?php
/**
 * 关键字过滤管理
 */
class keywords
{
    private $db;
    private $table;
    public $pages;
    public $error;

    public function __construct() {
        global $db;
        $this->db = &$db;
        $this->table = DB_PRE.'keywords';
    }

    /**
     * 关键字列表
     * @author menghao
     * @param <string> $word
     * @param <int> $page
     * @param <int> $pagesize
     * @return <array>
     */
    public function listinfo($word, $page=1, $pagesize=20)
    {
        $sql = "select * from $this->table where status=?";
        if(isset($word) && !empty($word))
        {
            $sql .= " and word like ?";
        }
        $page = max(intval($page),1);
        $offset = ($page - 1) * $pagesize;
        $limit = " limit $offset, $pagesize";
        $order = " order by id desc";
        $this->db->prepare($sql.$order.$limit);
        $this->db->set_int(STATUS_NORMAL);
        if(isset($word) && !empty($word))
        {
            $this->db->set_string("%$word%");
        }
        $data = $this->db->execute();

        $this->db->prepare("select count(*) count ".stristr($sql, 'from'));
        $this->db->set_int(STATUS_NORMAL);
        if(isset($word) && !empty($word))
        {
            $this->db->set_string("%$word%");
        }
        $res = $this->db->execute(true);
        $count = $res['count'];
        $this->pages = pages($count, $page, $pagesize);
        return $data;
    }

    /**
     * 添加关键字
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    public function add($data)
    {
        if(!$this->check($data))
        {
            return false;
        }
        return $this->db->insert($this->table, $data);
    }

    /**
     * 修改关键字
     * @author menghao
     * @param <int> $id
     * @param <array> $data
     * @return <boolean>
     */
    public function edit($id, $data)
    {
        if(!$this->check($data))
        {
            return false;
        }
        return $this->db->simple_update($this->table, $data, array('id'=>$id));
    }

    /**
     * 校验
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    private function check($data)
    {
        global $response;
        if(!isset($data['word']) || empty($data['word']))
        {
            $this->error = "请填写关键字";
            $response->text_alert('error', 'word', $this->error);
        }
        elseif(strlen($data['word']) > 255)
        {
            $this->error = "关键字长度不能超过255字节";
            $response->text_alert('error', 'word', $this->error);
        }

        if(strlen($data['replace']) > 255)
        {
            $this->error = "替换字长度不能超过255字节";
            $response->text_alert('error', 'replace', $this->error);
        }
        
        if(empty($this->error))
        {
            return true;
        }
        $response->real_execute();
        return false;
    }

    /**
     * 删除关键字
     * @author menghao
     * @param <int> $id
     * @return <boolean>
     */
    public function delete($id)
    {
        return $this->db->fake_delete($this->table, array('id'=>$id));
    }

    /**
     * 批量删除关键字
     * @author menghao
     * @param <array> $ids
     * @return <boolean>
     */
    public function delete_all($ids)
    {
        if(!is_array($ids)) return false;
        $ids = implode(',', $ids);
        $this->db->prepare("update $this->table set status=? where id in ($ids)");
        $this->db->set_int(STATUS_DEL);
        return $this->db->execute();
    }

    /**
     * 获取一条数据
     * @author menghao
     * @param <int> $id
     * @return <array>
     */
    public function get_one($id)
    {
        return $this->db->get_one($this->table, $id);
    }

    /**
     * 关键字过滤
     * @author menghao
     * @param <string> $content
     * @return <string>
     */
    public function strip($content)
    {
        $keywords = $this->db->query("select * from $this->table");
        $tran = array();
        if(is_array($keywords))
        {
            foreach($keywords as $word)
            {
                $replace = trim($word['replace']);
                $tran[$word['word']] = !empty($replace) ? $replace : str_repeat('*', mb_strlen($word['word'], CHARSET));
            }
        }
        return strtr($content, $tran);
    }
}
?>
